<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('线下采购订单')"/>
    <th:block th:include="include :: businessCSS"/>
    <style>
        .layui-table-tool-self {
            display: none;
        }

        .layui-table-tool-temp {
            padding-right: 0
        }
    </style>
</head>
<body class="no-skin">
<div class="main-container" id="main-container">
    <div class="main-content">
        <form class="layui-form head-form">
            <div class="north">
                <th:block th:include="include :: business_btn"/>
                <th:block th:include="include :: business_tpl('top')"/>
            </div>
            <div class="mid-content">
                <table class="layui-hide" id="dhtable" lay-filter="ftable"></table>
            </div>
            <th:block th:include="include :: pageFooter"/>
        </form>

    </div>
</div>
<script type="text/html" id="toolbar">
    <div style="padding: 16px;display: flex;justify-content: space-between;">
        <div style="font-weight: 700;font-size: 18px;">采购列表</div>
        <div>
            <button class="layui-btn layui-btn-normal dhicon"
                    th:classappend="${enableEdit} == 1 ?'':'layui-btn-disabled'" type="button"
                    th:disabled="${enableEdit} == 1 ?false:true" lay-event="addbtn" id="btn-sh">新增
            </button>
            <button class="layui-btn layui-btn-danger dhicon"
                    th:classappend="${enableEdit} == 1 ?'':'layui-btn-disabled'" type="button"
                    th:disabled="${enableEdit} == 1 ?false:true" lay-event="mjbtn_save" id="btn-sh">删除
            </button>
        </div>
    </div>
</script>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: businessJS"/>
<th:block th:include="include :: businessTableBtn"/>
<th:block th:include="include :: inlineJS"/>
<script type="text/javascript">
    var $, layer, form, table, laydate, element;//定义layui模块
    var listData1 = [];
    eachFields();//明细表格中的字段遍历


    if (!!$("#fXsFpsl_select").val()) $("#fXsFpsl").val($("#fXsFpsl_select").val());//默认税率赋值至input
    layui.use(['laydate', 'table', 'form', 'layer', 'jquery', 'util', 'element'], function () {
        $ = layui.$, layer = layui.layer, form = layui.form, table = layui.table, laydate = layui.laydate, util = layui.util, element = layui.element;
        beforeInit();//表单初始化之前的动作集合
        layui.element.render();
        $.get(moduleUrl + "/listbynid?nid=" + nid, function (data) {
            $.each(data.data, function (k, v) {
                dataStr.push(v);
            });
            listData1 = data.data
            // numall()
            initTable();//初始化表格
        });
        toolbarEvent();//通用表格头部工具栏事件监听
        toolEvent('ftable');//行event事件监听
        monitorSort();//监听排序——重载赋值渲染

        //监听单元格编辑
        table.on('edit(ftable)', function (obj) {
            var cost2 = obj.data.sum * obj.data.price2
            var serviceCharge = (+(obj.data.freightCharge) + (+cost2)) * 0.05
            obj.update({orderAmount: cost2, cost2: cost2, serviceCharge: serviceCharge.toFixed(2)});

            var dataArr = table.cache["dhtable"]
            var numall = 0
            dataArr.map((v) => {
                // v.serviceCharge=((+v.orderAmount+num)*0.05).toFixed(2)
                numall = numall + v.orderAmount
            })
            var fc = $('#freightCharge').val() ? $('#freightCharge').val() : 0
            numall = numall + (fc * 1)
            $('#orderAmountAll').val(numall.toFixed(2))//订单总金额

            $('#serviceCharge').val((numall * 0.05).toFixed(2))//采购服务费
        });


        $('body').on('blur', '#freightCharge', function () {
            var classtext = $(this).attr('class')
            if (classtext.indexOf('canntEdit') < 0) {
                var num = +$(this).val()
                var dataArr = table.cache["dhtable"]
                var numall = 0
                dataArr.map((v) => {
                    numall = numall + v.orderAmount
                })
                let fc = +numall + (num * 1)
                $('#orderAmountAll').val(fc.toFixed(2))//订单总金额
                $('#serviceCharge').val((fc * 0.05).toFixed(2))//采购服务费
                // table.reload('dhtable', {data: dataArr});
            }
        })

        table.on('toolbar(ftable)', function (obj) {
            var selectedObject = ""
            table.cache.dhtable.map((v, i) => {
                if (selectedObject) {
                    selectedObject += ',' + v.planID
                } else {
                    selectedObject += v.planID
                }
            })
            switch (obj.event) {
                case 'addbtn':
                    layer.open({
                        type: 2,
                        area: [($(window).width() - 200) + 'px', ($(window).height() - 100) + 'px'],
                        fix: false,//不固定
                        offset: '10%',//距离顶部
                        maxmin: true,//最大最小化
                        shade: 0.3,//遮罩
                        title: '采购列表',
                        content: "/purchase/purchaseplan/select2?pagetype=checkbox&by=direct&fmoduleTable=" + moduleTable + "&selectedObject=" + selectedObject,//+ "&isJp=" + $('#isJp').val()  23.05.31 去掉精品过滤
                        btn: ['确定', '取消'],
                        shadeClose: true,// 弹层外区域关闭
                        yes: function (index, layero) {
                            console.log('click---yes');
                            var res = window["layui-layer-iframe" + index].callbackdata();
                            if (res.length > 0) {
                                var bidstr = ""
                                $.each(res, function (i, v) {
                                    if (bidstr) {
                                        bidstr += ',' + v.bid
                                    } else {
                                        bidstr += v.bid
                                    }

                                })
                                layer.closeAll();
                                if (bidstr) {
                                    // $.get('/purchase/purchaseplan/listzjAll?bid=' + bidstr+'&isJp='+$('#isJp').val(), function (res) {
                                    $.get('/purchase/purchaseplan/listzjAll?bid=' + bidstr, function (res) {
                                        var numall = 0
                                        $.each(res.data, function (i, v) {
                                            v.planID = v.pBid,
                                                v.code = v.fBm,
                                                v.sName = v.name,
                                                v.name = v.spMc,
                                                v.sCode = v.sBm,
                                                v.sum = v.allNum,
                                                v.price = v.prices,
                                                v.cost = v.totalPrices,
                                                v.price2 = v.prices,
                                                v.cost2 = v.totalPrices,
                                                v.orderAmount = v.totalPrices,
                                                v.pGuid = v.guid,
                                                v.iid = i,
                                                $('#isJp').val() === "否" ? v.jpHbJe = 0 : v.jpHbJe


                                            // v.serviceCharge=((+$('#freightCharge').val()+(+v.totalPrices))*0.05).toFixed(2)
                                            v.vDFilesDefault = ''
                                            numall = numall + (+v.totalPrices)

                                        })
                                        $('#orderAmountAll').val((numall + (+$('#freightCharge').val())).toFixed(2))
                                        $('#serviceCharge').val((numall * 0.05).toFixed(2))
                                        table.reload('dhtable', {data: res.data, limit: res.data.length});
                                    });
                                }
                            } else {
                                layer.msg("请选择数据");
                            }
                        }
                    });
                    break;
                case 'mjbtn_save':
                    var objdata = table.checkStatus('dhtable')
                    objdata = objdata.data
                    listData1 = table.cache.dhtable;
                    if (objdata.length > 0) {
                        objdata.forEach(item => {
                            listData1 = listData1.filter(items => items.guid != item.guid);
                        });
                        table.reload('dhtable', {data: listData1});
                    } else {
                        layer.msg("请选择数据");
                    }
                    break;
            }
            ;
        });
    })

    //  赋值采购订单金额
    function numall() {
        var num = 0
        listData1.map((n, m) => {
            num = num + (+n.cost2)
        })
        $('#orderAmount').val(num)
    }

    // function getMoreAfter(fieldsList,fields,tindex,fieldName,fieldSearchType,isSearch,isLayer,res){//弹出框赋值回调
    // 	console.log("回调",res,fields)
    // 	if (fields === "planID"){
    // 		$.get("/purchase/purchaseplan/sonlist?fNid="+res.nid+"&fBm="+res.spBm,function(res){
    // 			let oldData =res.data.sonList;
    // 			let newData = [];
    // 			oldData.map((i)=>{
    // 				let x = {
    // 					code : i.fBm,
    // 					name : i.spMc,
    // 					sCode : i.sBm,
    // 					sName : i.name,
    // 					sum : i.cgSl,
    // 					price : i.prices,
    // 					cost : i.totalPrices,
    // 					price2 : i.prices,
    // 					cost2 : i.totalPrices
    // 				}
    // 				newData.push(x);
    // 			})
    // 			listData1 = newData
    // 			numall()
    // 			table.reload('dhtable', {data:newData});
    // 		});
    // 	}

    // }
</script>
</body>
</html>
